package com.yrihr.wisdom.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yrihr.wisdom.common.ResponseBody;
import com.yrihr.wisdom.common.ResponseCode;
import com.yrihr.wisdom.dto.TypeVo;
import com.yrihr.wisdom.dto.UploadPageDto;
import com.yrihr.wisdom.entity.ColumnType;
import com.yrihr.wisdom.entity.FileUpload;
import com.yrihr.wisdom.entity.UploadEntity;
import com.yrihr.wisdom.service.FileUploadService;
import com.yrihr.wisdom.util.RSAProperties;
import com.yrihr.wisdom.util.RSAUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName : FileUploadController
 * @Description :
 * @Author : wind
 * @Date: 2020-12-01 10:25
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
public class FileUploadController {

    private final FileUploadService fileUploadService;
    @Value("${uploadFilePath}")
    private String path;

    //使用默认路径
    @RequestMapping("/api/upload")
    public ResponseBody upload(String info,MultipartFile[] file) throws Exception {
        String name = "";
        String depiction = "";
        Integer columnType = 0;
        FileUpload fileUpload = new FileUpload();
        if (info != null) {
            ObjectMapper mapper = new ObjectMapper();
            fileUpload = mapper.readValue(info,FileUpload.class);
            name = fileUpload.getName();
            depiction = fileUpload.getDepiction();
            columnType = fileUpload.getColumnType();
        }
        Integer upload = fileUploadService.upload(file, path, name, depiction,columnType);
        if (upload == file.length && upload!=0) {
            return ResponseBody.success(ResponseCode.SUCCESS, upload);
        } else {
            return ResponseBody.error(ResponseCode.ERROR, null);
        }
    }

    /**
     * 查询所有类型
     * @return
     */
    @RequestMapping("/api/selectAllType")
    public ResponseBody selectAllType(){
        List<ColumnType> columnTypes = fileUploadService.selectAllType();
        if (columnTypes != null) {
            return ResponseBody.success(ResponseCode.SUCCESS, columnTypes);
        } else {
            return ResponseBody.error(ResponseCode.ERROR, null);
        }
    }

    /**
     * 根据type查询upload列表
     * @param typeVo
     * @return
     * @throws Exception
     */
    @RequestMapping("/api/upload/list")
    public ResponseBody selectUploadEntityList(@RequestBody TypeVo typeVo) throws Exception {
        if (typeVo != null) {
            Integer type = typeVo.getType();
            List<UploadEntity> list = fileUploadService.selectUploadEntityList(type);
            if (list != null && list.size() > 0) {
                return ResponseBody.success(ResponseCode.SUCCESS, list);
            } else {
                return ResponseBody.error(ResponseCode.ERROR, null);
            }
        } else {
            return ResponseBody.error(ResponseCode.ERROR, null);
        }
    }

    //下载
    @PostMapping("/api/upload/download")
    public void downloadFile(HttpServletResponse response, @RequestBody JSONObject id) throws Exception {
        String decryptId = "";
        UploadEntity uploadEntity = null;
        if (StringUtils.isNotBlank(id+"")) {
            String str = id.get("id").toString();
            //decryptId = RSAUtils.decryptByPrivateKey(RSAProperties.privateKey, "c5NpjHksOr+6+b7WTfpxpXLyJPVPzkc65t3FI0pf7LZloljn/7+HDzSMQNU44bRVtqUJPI8MaQykm7swf1vEcQ==");
            decryptId = RSAUtils.decryptByPrivateKey(RSAProperties.privateKey, str);
            uploadEntity = fileUploadService.checkUploadEntityId(decryptId);
            if (uploadEntity != null) {
                fileUploadService.download(response, decryptId);
            }
        }
    }

    /**
     * 根据id 查询uploadEntry 信息
     * @param typeVo
     * @return
     * @throws Exception
     */
    @RequestMapping("/api/upload/details")
    public ResponseBody selectUploadEntityById(@RequestBody TypeVo typeVo) throws Exception {
        if (typeVo != null) {
            long id = typeVo.getId();
            UploadEntity up = fileUploadService.selectUploadEntityById(id);
            if (up != null ) {
                return ResponseBody.success(ResponseCode.SUCCESS, up);
            } else {
                return ResponseBody.error(ResponseCode.ERROR, null);
            }
        } else {
            return ResponseBody.error(ResponseCode.ERROR, null);
        }
    }

    @RequestMapping("/api/upload/details/page")
    public ResponseBody selectPageUploadEntity(@RequestBody UploadPageDto page) throws Exception {
        if (page != null) {
           long current = page.getCurrent();
           long size = page.getSize();
            Integer type = page.getType();
            IPage<UploadEntity> up = fileUploadService.selectPage(current, size,type);
            if (up!=null ) {
                return ResponseBody.success(ResponseCode.SUCCESS, up);
            } else {
                return ResponseBody.error(ResponseCode.ERROR, null);
            }
        } else {
            return ResponseBody.error(ResponseCode.ERROR, null);
        }
    }
}
